/**
 * 
 */
package edu.sjtu.seda.main;

import java.util.ArrayList;
import java.util.List;

import edu.sjtu.seda.event.Event;
import edu.sjtu.seda.event.EventHandler;
import edu.sjtu.seda.stage.Stage;
import edu.sjtu.seda.stage.StageConfig;
import edu.sjtu.seda.stage.StageConfigImpl;
import edu.sjtu.seda.stage.StageImpl;

/**
 * @author john.zhu
 *
 */
public class StagePlatform {
	private List<Stage> stages = new ArrayList<Stage>();
	
	public void init(){
		
	}
	
	public Stage buildStage(String name,EventHandler handler){
		return buildStage(name,handler,new StageConfigImpl());
	}
	
	public Stage buildStage(String name,EventHandler handler,StageConfig config){
		List<Stage> st = new ArrayList<Stage>();
		if(stages!=null&&!stages.isEmpty()){
			for(Stage stage : stages){
				if(name.equals(stage.getName())){
					st.add(stage);
				}
			}
		}
		stages.removeAll(st);
		Stage stage = new StageImpl(name, handler,config);
		stages.add(stage);
		return stage;
	}
	
	public List<Stage> getStageList(){
		return stages;
	}
	
	public Stage getStage(String name){
		if(stages!=null&&!stages.isEmpty()){
			for(Stage stage : stages){
				if(name.equals(stage.getName())){
					return stage;
				}
			}
		}
		return null;
	}
	
	public void start(){
		if(stages!=null&&!stages.isEmpty()){
			for(Stage stage : stages){
				stage.start();
			}
		}
	}
	
	public void pushEvent(Event event){
		Stage stage = event.nextLocation();
		if(stage!=null){
			stage.getStageWrapper().getEventQueue().pushEvent(event);
		}
	}
	
	public void destroy(){
		if(stages!=null&&!stages.isEmpty()){
			for(Stage stage : stages){
				stage.destroy();
			}
		}
	}

}
